<?php

namespace Usap\Service\Operation;


class area
{


    public function getListByLevel($level)
    {
        $model = D("Usap/Common/Areas");
        return $model->where(['chk' => $level])->getField('area_id,name');
    }

    /**
     * 获取所有可做父级选择的区域配置
     */
    public function getParentAreasConifg()
    {
        $model = D("Usap/Common/Areas");
        return $model->where(['chk' => 1])->getField('area_id,name');
    }

    /**
     * 获取所有可做父级选择的区域配置
     */
    public function get_area_config()
    {
        $model = D("Usap/Common/Areas");
        return $model->getField('area_id,name');
    }


    /**
     * 添加区域信息
     * @param array() $data
     */
    public function addAreas($data)
    {
        $model = D("Usap/Common/Areas");
        return $model->add($data);
    }


    public function updateArea($id, $data)
    {
        $model = D("Usap/Common/Areas");
        return $model->where(['area_id' => $id])->save($data);
    }


    public function deletArea($id)
    {
        $model = D("Usap/Common/Areas");
        D("Usap/Common/AreaManager")->where(['area_id' => $id])->delete();
        $model->where(['area_id' => $id])->delete();
        return array('code' => '0000', 'msg' => '删除成功');
    }

    /**
     * 添加区域管理员信息
     */
    public function addAreaManage($data)
    {
        $model = D("Usap/Common/AreaManager");
        return $model->addAll($data);
    }


    /**
     * 获取区域管理员信息
     */
    public function getAreaManageInfo($id)
    {
        $model = D("Usap/Common/AreaManager");
        return $model->where(['area_id' => $id])->select();
    }


    public function deletAreaManage($id)
    {
        $model = D("Usap/Common/AreaManager");
        return $model->where(['area_id' => $id])->delete();
    }


    /**
     * @param int $area_id
     * 通过主键area_id 获取地区信息
     */
    public function getInfoById($area_id)
    {
        $model = D("Usap/Common/Areas");
        return $model->where(['area_id' => $area_id])->find();
    }
    /**
     * @param int $parent_id
     * $parent_id 获取地区信息
     */
    public function getInfoByParentId($parent_id)
    {
        $model = D("Usap/Common/Areas");
        return $model->where(['parent_id' => $parent_id])->select();
    }

    /**
     * 获取区域列表
     * @param $where
     *
     */
    public function get_areas_list($where)
    {
        $model = D("Usap/Common/Areas");
        $count = $model->where($where)->count();
        $list = [];
        if ($count > 0) {
            $list = $model->where($where)->select();
        }
        $result = [
            'count' => $count ? $count : 0,
            'list' => $list
        ];
        return $result;
    }


}